package org.dromara.oce.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.oce.domain.bo.OcePromotionAddEverydayCensusBo;
import org.dromara.oce.domain.dto.OcePromotionAddEverydayCensusDto;
import org.dromara.common.core.domain.dto.TypeDto;
import org.dromara.oce.domain.vo.OcePromotionAddEverydayCensusVo;
import org.dromara.oce.service.OcePromotionAddEverydayCensusService;
import org.dromara.oce.utils.PlatformParamTransUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 广告每日新增数量统计
 *
 * @author Lion Li
 * @date 2024-12-24
 * @module 管理平台接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oce/promotionAddEverydayCensus")
public class OcePromotionAddEverydayCensusController extends BaseController {

    private final OcePromotionAddEverydayCensusService ocePromotionAddEverydayCensusService;

    /**
     * 查询广告每日新增数量统计列表
     */
    @SaCheckPermission("oce:promotionAddEverydayCensus:list")
    @GetMapping("/list")
    public TableDataInfo<OcePromotionAddEverydayCensusDto> list(OcePromotionAddEverydayCensusBo bo, PageQuery pageQuery) {
        if(StringUtils.isBlank(pageQuery.getOrderByColumn())){
            pageQuery.setOrderByColumn("promotionAddCensusId");
            pageQuery.setIsAsc("desc");
        }
        Long deptId = LoginHelper.getDeptId();
        if(!LoginHelper.isSuperAdmin() && !LoginHelper.isTenantAdmin()){
            bo.setDeptId(deptId);
        }
        Long deptIdCache = PlatformParamTransUtil.getDynamicDept();
        if (deptIdCache > 0L) {
            bo.setDeptId(deptIdCache);
        }
        TableDataInfo<OcePromotionAddEverydayCensusDto> tableDataInfo = new TableDataInfo<>();
        List<TypeDto> sum = new ArrayList<>();
        List<OcePromotionAddEverydayCensusDto> dtos = new ArrayList<>();
        List<OcePromotionAddEverydayCensusVo> sumList = ocePromotionAddEverydayCensusService.queryList(bo);
        if(CollectionUtils.isNotEmpty(sumList)){
            int sumNum = sumList.stream().map(OcePromotionAddEverydayCensusVo::getPromotionNum).filter(Objects::nonNull).reduce(0, Integer::sum);
            TypeDto typeDto = new TypeDto();
            typeDto.setType(sumNum + "");
            typeDto.setDesc("广告新增数量");
            sum.add(typeDto);
        }
        tableDataInfo.setSum(sum);
        TableDataInfo<OcePromotionAddEverydayCensusVo> dataInfo = ocePromotionAddEverydayCensusService.queryPageList(bo, pageQuery);
        List<OcePromotionAddEverydayCensusVo> list = dataInfo.getRows();
        if(CollectionUtils.isNotEmpty(list)){
            for (OcePromotionAddEverydayCensusVo vo : list) {
                OcePromotionAddEverydayCensusDto dto = new OcePromotionAddEverydayCensusDto();
                dto.setCreateName(vo.getCreateName());
                dto.setThatdayDate(vo.getThatdayDate());
                dto.setPromotionNum(vo.getPromotionNum());
                dtos.add(dto);
            }
        }
        tableDataInfo.setCode(dataInfo.getCode());
        tableDataInfo.setMsg(dataInfo.getMsg());
        tableDataInfo.setRows(dtos);
        tableDataInfo.setTotal(dataInfo.getTotal());
        return tableDataInfo;
    }

    /**
     * 获取广告每日新增数量统计详细信息
     *
     * @param promotionAddCensusId 主键
     */
    @SaCheckPermission("oce:promotionAddEverydayCensus:query")
    @GetMapping("/{promotionAddCensusId}")
    public R<OcePromotionAddEverydayCensusVo> getInfo(@NotNull(message = "主键不能为空")
                                                      @PathVariable Long promotionAddCensusId) {
        return R.ok(ocePromotionAddEverydayCensusService.queryById(promotionAddCensusId));
    }

    /**
     * 新增广告每日新增数量统计
     */
    @SaCheckPermission("oce:promotionAddEverydayCensus:add")
    @Log(title = "广告每日新增数量统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OcePromotionAddEverydayCensusBo bo) {
        return toAjax(ocePromotionAddEverydayCensusService.insertByBo(bo));
    }

    /**
     * 修改广告每日新增数量统计
     */
    @SaCheckPermission("oce:promotionAddEverydayCensus:edit")
    @Log(title = "广告每日新增数量统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OcePromotionAddEverydayCensusBo bo) {
        return toAjax(ocePromotionAddEverydayCensusService.updateByBo(bo));
    }

    /**
     * 删除广告每日新增数量统计
     *
     * @param promotionAddCensusIds 主键串
     */
    @SaCheckPermission("oce:promotionAddEverydayCensus:remove")
    @Log(title = "广告每日新增数量统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{promotionAddCensusIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] promotionAddCensusIds) {
        return toAjax(ocePromotionAddEverydayCensusService.deleteWithValidByIds(List.of(promotionAddCensusIds), true));
    }
}
